package yanyan.com.tochar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.baidu.mobads.AppActivity;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import androidx.annotation.Nullable;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.DialogUtil;
import yanyan.com.tochar.utils.EmptyUtils;
import yanyan.com.tochar.utils.ToastUtil;

/**
 * 重复名称生成
 */
public class FunsRepeatGameNmActivity extends AppActivity {
    private EditText oldGameName,newGameName;
    private DialogUtil dialogUtil=null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funs_gamenm);
       oldGameName = (EditText)findViewById(R.id.game_name);
        newGameName=(EditText)findViewById(R.id.game_new_name);
        //统计各功能
        CoreUtil.statisticalFuns(this,"funs_game_name");
    }

    /**
     *  从这里依次排序,可以得更多,为防盗用,每次更新放出一部分
     * %E2%98%91
     * %E2%98%92
     * %E2%98%93
     * %E2%98%98
     * %E2%98%99
     * %E2%98%9a
     * %E2%98%9b
     * %E2%98%a0
     * %E2%98%a1
     * %E2%98%a2
     * %E2%98%a3
     * %E2%98%a4
     * %E2%98%a5
     * %E2%98%a6
     * %E2%98%a7
     * %E2%98%a8
     * %E2%98%a9
     * 26  %E2%98%91
     * 30 %E2%9C%96
     * 54 %E2%9E%97
     * 55 %E2%9E%97
     */
//    private String[] codes={"%F0%9F%87%A6","%F0%9F%87%A7","%F0%9F%87%A8","%F0%9F%87%A9",
//            "%F0%9F%87%AA","%F0%9F%87%AB","%F0%9F%87%AC","%F0%9F%87%AD",
//            "%F0%9F%87%AE","%F0%9F%87%AF","%F0%9F%87%B0","%F0%9F%87%B1"
//            ,"%F0%9F%87%B2","%F0%9F%87%B3","%F0%9F%87%B4",
//            "%F0%9F%87%B5","%F0%9F%87%B6","%F0%9F%87%B7","%F0%9F%87%B8"
//            ,"%F0%9F%87%B9","%F0%9F%87%BA","%F0%9F%87%BB","%F0%9F%87%BC"
//            ,"%F0%9F%87%BD","%F0%9F%87%BE","%F0%9F%87%BF","%E2%98%91","%E2%9E%97","%E2%9E%95","%E2%9E%96",
//            "%E2%9C%96","%E2%9B%8E","%E2%9E%BF","%F0%9F%86%95","%E2%8F%AA",
//            "%F0%9F%88%B9","%F0%9F%88%B5","%F0%9F%86%92","%F0%9F%88%B8","%F0%9F%88%B3",
//            "%F0%9F%88%82","%F0%9F%85%B0","%F0%9F%88%B7","%F0%9F%86%8E","%F0%9F%88%AF",
//            "%F0%9F%88%B6","%F0%9F%85%BE","%F0%9F%88%81","%F0%9F%9A%BE","%F0%9F%88%BA"
//            ,"%F0%9F%88%9A","%F0%9F%86%9A","%F0%9F%85%B1","%F0%9F%86%94","%E2%9C%96",
//            "%E2%98%91","%E2%9E%97","%E2%9E%95","%E2%9E%96","%E2%9B%8E","%E2%8F%AA","%E2%9E%BF","%F0%9F%88%BA","%F0%9F%9A%BE"};
    private String[] codes={"%E2%98%91","%E2%98%92","%E2%98%93","%E2%98%98"," %E2%98%99","%E2%98%9a",
    "%E2%98%9b","%E2%98%a0","%E2%98%a1","%E2%98%a2","%E2%98%a3","%E2%98%a4","%E2%98%a5","%E2%98%a6","%E2%98%a7","%E2%98%a8","%E2%98%a9"};
public void getName(View view){
    String ganeName = oldGameName.getText().toString();
    if(EmptyUtils.isEmpty(ganeName)){
        ToastUtil.showShotToast(this,"请输入游戏名字");
        return;
    }
    String okName="";
   if(ganeName.length()>4){
    int insert= CoreUtil.randomNumber(0,ganeName.length());
       int codeIndex= CoreUtil.randomNumber(0,codes.length-1);
       String code= toChar(codes[codeIndex]);
       okName=insertOneToStr(insert,ganeName,code);

   }else {
       int insert= CoreUtil.randomNumber(0,ganeName.length());
       int codeIndex= CoreUtil.randomNumber(0,codes.length-1);
       String code= toChar(codes[codeIndex]);
       okName=insertOneToStr(insert,ganeName,code);
      //第二次
       insert= CoreUtil.randomNumber(0,okName.length());
      codeIndex= CoreUtil.randomNumber(0,codes.length-1);
      code= toChar(codes[codeIndex]);
       okName=insertOneToStr(insert,okName,code);
   }

    CoreUtil.copy(okName,this);
   newGameName.setText(okName);
    dialogUtil=new DialogUtil(this, "生成成功已复制", "名字:" + okName + "\n已自动复制,如果名字出现重复请再次生成", "知道了",
            "", null,null,true);
    dialogUtil.show();

}

private String insertOneToStr(int index,String str,String code){
    String[] strs = str.split("");
    String res="";
    if(index>=strs.length){
        return str+code;
    }
    for (int i=0;i<strs.length;i++){
        if(index==i){
            res+=code;
        }
        res+=strs[i];
    }
    return res;
}

    /**
     * 编码转字符
     * @param code
     * @return
     */
    private String toChar(String code)
    {
        try {
            code= URLDecoder.decode(code, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return code;
    }
}
